package com.Drools.CIPS.controller;

import com.Drools.CIPS.bean.Rulers;
import com.Drools.CIPS.ruler.MTRuler;
import com.Drools.CIPS.sercice.DroolsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;


@RequestMapping("/drools")
@Controller
public class DroolsController {
    @Resource
    DroolsService droolsService;

    @RequestMapping("/addDrools")
    public String addDrools(Rulers rulers, Model model,RedirectAttributes ra){
        System.out.println(rulers.ruler());
        MTRuler.addRuler(rulers);
        droolsService.addDrools(rulers);
        model.addAttribute("msg","成功");
        ra.addAttribute("msg", System.currentTimeMillis());
        return "redirect:/add";//重定向
    }
    @RequestMapping("/deleteDrools")//删除规则
    public String deleteDrools(String fileName, Model model,RedirectAttributes ra){

        droolsService.update(fileName);//删除规则-将规则设为无效
        String s = MTRuler.deleteRuler(fileName);//删除规则
        model.addAttribute("msg",s);
        ra.addAttribute("msg", s + System.currentTimeMillis());
        return "redirect:/add";//重定向
    }

    @RequestMapping("/deleteRuler")//删除规则
    public String deleteDrools(String packageName,String ruleName, Model model,RedirectAttributes ra){
        if(packageName==null||packageName.trim().equals("")){
            return "redirect:/add";//重定向
        }
        droolsService.update2(packageName,ruleName);//删除规则-将规则设为无效
        String s = MTRuler.deleteRuler(packageName,ruleName);//删除规则
        model.addAttribute("msg",s);
        ra.addAttribute("msg", s + System.currentTimeMillis());
        return "redirect:/add";//重定向
    }

}
